/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.aol.expressions.expressions.service;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URISyntaxException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Administrator
 */
public class MockSearchExpressionsHandler extends HttpServlet {
   
    /** 
    * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
    * @param request servlet request
    * @param response servlet response
    */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException, URISyntaxException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            String type = request.getParameter("type");
            String term = request.getParameter("term");
            String page = request.getParameter("page");
            String f = request.getParameter("f");  
           
            if (f != null && f.equals("json"))
            {
            
             if (type != null)
                {
                    if (type.equals("buddyIcon"))
                    {
                        out.print(getTextFromFile("searchExpressionsIcon.txt"));
                       
                    }
                   
                    if(type.equals("imChrome"))
                    {
                        out.print(getTextFromFile("searchExpressionsWallpaper.txt"));
                    }
                 
                    if(type.equals("imSound"))
                    {
                        out.print(getTextFromFile("searchExpressionsSound.txt"));
                    }
                    
                    if(type.equals("emoticon"))
                    {
                        out.print(getTextFromFile("searchExpressionsSmiley.txt"));
                    }
                     
                    if(type.equals("oneclicksubcat"))
                    {
                        out.print(getTextFromFile("searchExpressionsSet.txt"));
                    }
                    
                 }  
                   
                  if (page!=null && !page.equals("1"))
                    {
                        out.print(getTextFromFile("searchExpressionsPage.txt"));
                      
                    }
                  if(term!=null&&term.equals("easter"))
                  {
                   out.print(getTextFromFile("searchExpressionsEster.txt"));
                  }
               }
            
        } finally { 
            out.close();
        }
    } 
 private String getTextFromFile(String fileName) throws URISyntaxException, FileNotFoundException, IOException
    {
        //BufferedReader in = new BufferedReader(new FileReader(
        //        new File(this.getClass().getClassLoader().getResource(fileName).toURI())));
        BufferedReader in = new BufferedReader(new FileReader("d:\\"+ fileName));
        String str;
        StringBuffer sb = new StringBuffer();
        while ((str = in.readLine()) != null) {
            sb.append(str);
        }
        in.close();
        return sb.toString();
    }
    // <editor-fold defaultstate="collapsed" desc="HttpServlet 方法。单击左侧的 + 号以编辑代码。">
    /** 
    * Handles the HTTP <code>GET</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (URISyntaxException ex) {
            Logger.getLogger(MockSearchExpressionsHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
    } 

    /** 
    * Handles the HTTP <code>POST</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (URISyntaxException ex) {
            Logger.getLogger(MockSearchExpressionsHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** 
    * Returns a short description of the servlet.
    */
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
